/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jsf;
import ejb.Product;
import bean.ProductBean;
import bean.WarehouseBean;
import cart.Cart;
import cart.CartItem;
import ejb.Warehouse;
import java.util.List;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
/**
 *
 * @author longdm
 */

public class ProductController {

    /** Creates a new instance of ProductController */
    public ProductController() {
        productDataModel = new ListDataModel();
        searchProduct = new ListDataModel();
        cartModel = new ListDataModel();
        cart = new Cart();
    }

    private ListDataModel productDataModel;
    private Product currentProduct;

    public Product getCurrentProduct() {
        return currentProduct;
    }

    public ListDataModel getProductDataModel() {
        ProductBean pb = new ProductBean();
        productDataModel.setWrappedData(pb.getAllProduct());
        return productDataModel;
    }

    public String viewProduct()
    {
        currentProduct = (Product) productDataModel.getRowData();
        return "productDetail";
    }

    public String editProduct()
    {
        ProductBean pb = new ProductBean();
        pb.editProduct(currentProduct);
        return "productManager";
    }

    public String deleteProduct()
    {
        currentProduct = (Product) productDataModel.getRowData();
        ProductBean pb = new ProductBean();
        pb.removeProduct(currentProduct);
        return "";
    }

    public String newProduct()
    {
        ProductBean pb = new ProductBean();
        currentProduct.setPicture("");
        pb.createProduct(currentProduct);
        return "productManager";
    }

    public String toProductEdit()
    {
        currentProduct = (Product) productDataModel.getRowData();
        return "productEdit";
    }

    public String toNewProduct()
    {
        currentProduct = new Product();
        return "newProduct";
    }

    public SelectItem[] getWarehouseSelectItems()
    {
        WarehouseBean wb = new WarehouseBean();
        List<Warehouse> whs = wb.getAllWarehouse();

        SelectItem[] items = new SelectItem[whs.size()];
        int i = 0;
        for(Warehouse w : whs)
        {
            items[i] = new SelectItem(w, w.getWarehouseName());
            i++;
        }
        return items;
    }


    private ListDataModel cartModel;
    private CartItem currentCartItem;
    private Cart cart;

    public ListDataModel getCartModel() {
        cartModel.setWrappedData(cart.getItems());
        return cartModel;
    }

    public CartItem getCurrentCartItem() {
        return currentCartItem;
    }

    public String addToCart(){
        currentProduct = (Product)productDataModel.getRowData();
        cart.addToCart(new CartItem(currentProduct, 1));
        return "";
    }

    public String viewCart(){
        return "_viewCart";
    }

    public String to_productList()
    {
        return "_productList";
    }

    String kword;

    public String getKword() {
        return kword;
    }

    public void setKword(String kword) {
        this.kword = kword;
    }

    private ListDataModel searchProduct;

    public ListDataModel getSearchProduct() {
        ProductBean pb = new ProductBean();
        searchProduct.setWrappedData(pb.searchByName(kword));
        return searchProduct;
    }


    public String _searchProduct()
    {
        //getSearchProduct();
        return "searchProductResult";
    }
}
